using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
    public PlayerInputControl inputControl;
    private Rigidbody2D rb;
    private CapsuleCollider2D capsuleCollider2D;
    private PhysicsCheck physicsCheck;
    public Vector2 inputDirection;
    public float speed;
    public float jumpForce;
    public bool isCrouch;
    public bool isHurt;
    public float hurtForce;
    public bool isDead;

    private Vector2 orignalColliderSize;
    private Vector2 orignalColliderOffset;

    private void Awake()
    {
        //private情况下rigidbody的获得方法
        rb = GetComponent<Rigidbody2D>();
        physicsCheck = GetComponent<PhysicsCheck>();
        inputControl = new PlayerInputControl();
        capsuleCollider2D = GetComponent<CapsuleCollider2D>();
        
        orignalColliderSize = capsuleCollider2D.size;
        orignalColliderOffset = capsuleCollider2D.offset;
        
        //事件注册+=
        inputControl.Gameplay.Jump.started += Jump;
    }


    private void OnEnable()
    {
        inputControl.Enable();
    }

    private void OnDisable()
    {
        inputControl.Disable();
    }

    private void Update()
    {
        inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
    }

    private void FixedUpdate()
    {
        if(!isHurt)
            Move();
    }
    
    // //测试
    // private void OnTriggerStay2D(Collider2D other)
    // {
    //     Debug.Log(other.name);
    // }

    public void Move()
    {
        isCrouch = inputDirection.y < -0.5f && physicsCheck.isGround;
        //人物移动
        if (!isCrouch)
        {
            //前面是x轴速度，先从rigidbody获取方向，然后乘以速度，后面为y轴速度，原本是重力，保持不变
            rb.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, rb.velocity.y);

            //如果修改人物比例这里也需要修改
            int faceDir = (int)transform.localScale.x;
            //人物翻转
            if (inputDirection.x < 0)
            {
                faceDir = -1;
            }
            else if (inputDirection.x > 0)
            {
                faceDir = 1;
            }
            transform.localScale = new Vector3(faceDir, 1, 1);
        }
        
        if (isCrouch)
        {
            //修改碰撞体大小和位移
            capsuleCollider2D.size = new Vector2(0.7f, 1.7f);
            capsuleCollider2D.offset = new Vector2(-0.05f, 0.85f);
            
            //下蹲后可能会有速度，所以需要将速度归零
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
        else
        {
            capsuleCollider2D.size = orignalColliderSize;
            capsuleCollider2D.offset = orignalColliderOffset;
            //恢复碰撞体大小和位移
        }
    }
    private void Jump(InputAction.CallbackContext obj)
    {
        //Debug.Log("Jump");
        if (physicsCheck.isGround)
        {
            rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
        }
    }

    public void GetHurt(Transform attacker)
    {
        isHurt = true;
        rb.velocity = Vector2.zero;
        Vector2 dir = new Vector2(transform.position.x - attacker.position.x, 0).normalized;
        rb.AddForce(dir * hurtForce, ForceMode2D.Impulse);
    }

    public void PlayerDead()
    {
        isDead = true;
        inputControl.Gameplay.Disable();
    }
}
